/////////////////////////////////////////////////////////////////////////////
// Name:        ax_supportedfiles.cpp
// Purpose:     A class for manipulating supported file types
// Author:      Julian Smart
// Modified by:
// Created:     2008-11-18
// RCS-ID:      $Id$
// Copyright:   (c) Julian Smart
// Licence:     New BSD License
/////////////////////////////////////////////////////////////////////////////

#include "wx/wx.h"
#include "wx/config.h"
#include "wx/checklst.h"

#include "ax_resource/ax_supportedfiles.h"

/*
 * Supported files
 */

void AxSupportedFiles::Copy(const AxSupportedFiles& supported)
{
    m_supportedFileExtensions = supported.m_supportedFileExtensions;
    m_supportedFileDescriptions = supported.m_supportedFileDescriptions;
    m_supportedFileStates = supported.m_supportedFileStates;
}

// Clear arrays
void AxSupportedFiles::Clear()
{
    m_supportedFileExtensions.Clear();
    m_supportedFileDescriptions.Clear();
    m_supportedFileStates.Clear();
}

// Load config info
bool AxSupportedFiles::LoadConfig(wxConfigBase& config)
{
    config.SetPath(wxT("SupportedFiles"));
    
    if (config.Exists(wxT("Extension0")))
    {
        Clear();
        int i = 0;
        while (true)
        {
            wxString typeKey(wxString::Format(wxT("Extension%d"), i));
            wxString descrKey(wxString::Format(wxT("Description%d"), i));
            wxString enabledKey(wxString::Format(wxT("Enabled%d"), i));
            wxString ext, descr;
            bool enabled = true;
            if (!config.Read(typeKey, & ext) || ext.IsEmpty())
            {
                break;
            }

            config.Read(descrKey, & descr);
            config.Read(enabledKey, & enabled);

            AddFileType(ext, descr, enabled);

            i ++;
        }
    }

    config.SetPath(wxT("/"));
    return true;
}

// Save config info
bool AxSupportedFiles::SaveConfig(wxConfigBase& config)
{
    config.SetPath(wxT("SupportedFiles"));
    int i;
    for (i = 0; i < (int) GetCount(); i++)
    {
        wxString typeKey(wxString::Format(wxT("Extension%d"), i));
        wxString descrKey(wxString::Format(wxT("Description%d"), i));
        wxString enabledKey(wxString::Format(wxT("Enabled%d"), i));

        config.Write(typeKey, GetExtension(i));
        config.Write(descrKey, GetDescription(i));
        config.Write(enabledKey, GetEnabled(i));
    }
    
    // Empty extension marks the final entry
    wxString typeKey(wxString::Format(wxT("Extension%d"), GetCount()));
    config.Write(typeKey, wxEmptyString);

    config.SetPath(wxT("/"));
    return true;
}

// Do some initialisation
void AxSupportedFiles::Init()
{
}

// Add a supported file type
bool AxSupportedFiles::AddFileType(const wxString& ext, const wxString& description, bool enabled)
{
    wxString ext1(ext.Lower());
    if (m_supportedFileExtensions.Index(ext1) == wxNOT_FOUND)
    {
        m_supportedFileExtensions.Add(ext1);
        m_supportedFileDescriptions.Add(description);
        m_supportedFileStates.Add(enabled);
    }

    return true;
}

// Enable file type
bool AxSupportedFiles::Enable(const wxString& ext, bool enabled)
{
    wxString ext1(ext.Lower());
    int idx = m_supportedFileExtensions.Index(ext1);
    if (idx != wxNOT_FOUND)
    {
        m_supportedFileStates[idx] = enabled ? 1 : 0;
        return true;
    }
    else
        return false;
}

// Is this type enabled?
bool AxSupportedFiles::IsEnabled(const wxString& ext) const
{
    wxString ext1(ext.Lower());
    int idx = m_supportedFileExtensions.Index(ext1);
    if (idx != wxNOT_FOUND)
    {
        return (m_supportedFileStates[idx] != 0);
    }
    else
        return false;
    return true;
}

// Does this type exist?
bool AxSupportedFiles::TypeExists(const wxString& ext) const
{
    wxString ext1(ext.Lower());
    int idx = m_supportedFileExtensions.Index(ext1);
    return (idx != wxNOT_FOUND);
}


// Transfer enabled states to and from checklistbox
bool AxSupportedFiles::TransferDataToCheckListBox(wxCheckListBox* checkListBox, bool recreateList)
{
    if (recreateList || checkListBox->GetCount() == 0)
    {
        checkListBox->Clear();
        size_t i;
        for (i = 0; i < GetCount(); i++)
        {
            checkListBox->Append(m_supportedFileDescriptions[i]);
            checkListBox->Check(i, m_supportedFileStates[i] != 0);
        }
    }
    else
    {
        size_t i;
        for (i = 0; i < GetCount(); i++)
        {
            checkListBox->Check(i, m_supportedFileStates[i] != 0);
        }
    }

    return true;
}

bool AxSupportedFiles::TransferDataFromCheckListBox(wxCheckListBox* checkListBox)
{
    size_t i;
    for (i = 0; i < GetCount(); i++)
    {
        m_supportedFileStates[i] = checkListBox->IsChecked(i) ? 1 : 0;
    }
    return true;
}

// Remove file type
bool AxSupportedFiles::RemoveFileType(int idx)
{
    m_supportedFileExtensions.RemoveAt(idx);
    m_supportedFileDescriptions.RemoveAt(idx);
    m_supportedFileStates.RemoveAt(idx);

    return true;
}

// Get a wildcard with all enabled extensions
wxString AxSupportedFiles::GetWildcard() const
{
    wxString wildcard;
    size_t i;
    for (i = 0; i < GetCount(); i++)
    {
        if (m_supportedFileStates[i] != 0)
        {
            if (!wildcard.IsEmpty())
                wildcard += wxT("|*.");
            wildcard += m_supportedFileDescriptions[i];
            wildcard += wxT(" (") + m_supportedFileExtensions[i] + wxT(")");
            wildcard += wxT("|*.")+ m_supportedFileExtensions[i];
        }
    }
    if (!wildcard.IsEmpty())
        wildcard += wxT("|");
    wildcard += wxT("All files (*.*)|*.*");

    return wildcard;
}

